package com.sgb.kjwl.view.ui.activity.supplier3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.swgk.core.view.entity.MenuEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 王涛
 * 时间：2021.2.18
 * 描述：查找供应商、找商品
 */
public class SupplierActivity4 extends AppActivity {

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    public static void start(Context context, boolean map, List<MenuEntity> data) {
        Intent intent = new Intent();
        intent.putExtra("map", map);
        intent.putExtra("data", (Serializable) data);
        intent.setClass(context, SupplierActivity4.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<MenuEntity> supplierMenuEntities = (List<MenuEntity>) getIntent().getSerializableExtra("data");
        List<MenuEntity> goodsMenuEntities = CopyValue(supplierMenuEntities);
        final Fragment[] fragments = {NewSupplierFragment.newInstance(getIntent().getBooleanExtra("map", true), supplierMenuEntities, "0"),
                NewGoodsMapFragment.newInstance(getIntent().getBooleanExtra("map", true), goodsMenuEntities, "0")};
        final String[] tableName = {"找供应商", "找商品"};
        com.sgb.kjwl.databinding.ActivityPickSigningUpListBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_signing_up_list);
        binding.include.liuhaiStatusView.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(this);
        binding.viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tableName[position];
            }
        });
        binding.include.tabLayout.setupWithViewPager(binding.viewPager);
        binding.viewPager.setCurrentItem(0);
        initShanyan();
        for (int i = 0; i < binding.include.tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = binding.include.tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i, tableName));
            }
        }

        binding.include.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        updateTabTextView(binding.include.tabLayout.getTabAt(binding.include.tabLayout.getSelectedTabPosition()), true);
    }

    private List<MenuEntity> CopyValue(List<MenuEntity> supplierMenuEntities) {
        List<MenuEntity> menuEntityList = new ArrayList<>();
        if (supplierMenuEntities != null || supplierMenuEntities.size() != 0) {
            for (MenuEntity menuEntity : supplierMenuEntities) {
                MenuEntity menuEntity1 = new MenuEntity();
                menuEntity1.setDatas(menuEntity.getDatas());
                menuEntity1.setUrl(menuEntity.getUrl());
                menuEntity1.setSelected(menuEntity.isSelected());
                menuEntity1.setParentId(menuEntity.getParentId());
                menuEntity1.setName(menuEntity.getName());
                menuEntity1.setImageResource(menuEntity.getImageResource());
                menuEntity1.setId(menuEntity.getId());
                menuEntity1.setIconContent(menuEntity.getIconContent());
                menuEntity1.setHostImg(menuEntity.getHostImg());
                menuEntity1.setAppIconUrl(menuEntity.getAppIconUrl());
                menuEntity1.setAppIcon(menuEntity.getAppIcon());
                menuEntityList.add(menuEntity1);
            }
        }

        return menuEntityList;
    }

    @SuppressLint("InflateParams")
    private View getTabView(int currentPosition, String[] tableName) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_textview, null);
        TextView textView = view.findViewById(R.id.tv_tab_item_textview);
        textView.setTextColor(ContextCompat.getColor(SupplierActivity4.this, R.color.white));
        textView.setTextSize(15);
        textView.setText(tableName[currentPosition]);
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (tab == null || tab.getCustomView() == null) {
            return;
        }
        if (isSelect) {
            //选中加粗
            TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextColor(ContextCompat.getColor(SupplierActivity4.this, R.color.color_main_bg));
            tabSelect.setTextSize(16);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(ContextCompat.getColor(SupplierActivity4.this, R.color.color_main_bg));
            tabUnSelect.setTextSize(15);
            tabUnSelect.setText(tab.getText());
        }
    }

    @Override
    public void onMultiClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }
}